package cn.otrue.patienthealthmanager.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.bean.Questions.QuestionResearchDetail;
import cn.otrue.patienthealthmanager.bean.Questions.QuestionResearchDetailSeletedItem;
import cn.otrue.patienthealthmanager.view.NoScrollListView;

public class QuestionnaireDetailAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<QuestionResearchDetail> questionResearchDetailList;// 问题列表
	private String isSelected;

	public QuestionnaireDetailAdapter(Context context,
			List<QuestionResearchDetail> questionResearchDetailList,
			String isSelected) {
		inflater = LayoutInflater.from(context);
		this.questionResearchDetailList = questionResearchDetailList;
		this.isSelected = isSelected;
	}

	@Override
	public int getCount() {
		return questionResearchDetailList != null ? questionResearchDetailList
				.size() : 0;
	}

	@Override
	public Object getItem(int arg0) {
		return questionResearchDetailList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		convertView = inflater.inflate(R.layout.layout_question_survey_yundong,
				null);
		// TextView tv_question_item_number = (TextView) convertView
		// .findViewById(R.id.tv_question_item_number);
		// tv_question_item_number.setText(questionResearchDetailList
		// .get(position).questionId);
		TextView tv_question_item_wenti = (TextView) convertView
				.findViewById(R.id.tv_question_item_wenti);
		tv_question_item_wenti
				.setText(questionResearchDetailList.get(position).questionName);
		NoScrollListView nslv_question_item_select = (NoScrollListView) convertView
				.findViewById(R.id.nslv_question_item_select);
		SelectItemAdapter adapter = new SelectItemAdapter(
				questionResearchDetailList.get(position).questionResearchDetailSeleteditemList);
		nslv_question_item_select.setAdapter(adapter);
		return convertView;
	}

	public class SelectItemAdapter extends BaseAdapter {

		private List<QuestionResearchDetailSeletedItem> questionResearchDetailSeleteditemList;

		public SelectItemAdapter(
				List<QuestionResearchDetailSeletedItem> questionResearchDetailSeleteditemList) {
			this.questionResearchDetailSeleteditemList = questionResearchDetailSeleteditemList;
		}

		@Override
		public int getCount() {
			return questionResearchDetailSeleteditemList != null ? questionResearchDetailSeleteditemList
					.size() : 0;
		}

		@Override
		public Object getItem(int arg0) {
			return questionResearchDetailSeleteditemList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup arg2) {
			convertView = inflater.inflate(R.layout.layout_questionnaire_detail_item, null);
			final CheckBox ckb_question_selectitem = (CheckBox) convertView.findViewById(R.id.ckb_question_selectitem);
			ckb_question_selectitem.setText(questionResearchDetailSeleteditemList.get(position).seleteditem_name);
			if (questionResearchDetailSeleteditemList.get(position).isChecked == 1) {
				ckb_question_selectitem.setChecked(true);
			} else {
				ckb_question_selectitem.setChecked(false);
			}
			ckb_question_selectitem.setOnClickListener(new Button.OnClickListener() {

				@Override
				public void onClick(View arg0) {
					if(ckb_question_selectitem.isChecked()){
						questionResearchDetailSeleteditemList.get(position).isChecked = 1;
					}else{
						questionResearchDetailSeleteditemList.get(position).isChecked = 0;
					}
				}
			});
			if ("1".equals(isSelected)) {
				ckb_question_selectitem.setEnabled(false);
			}
			return convertView;
		}

	}

}
